package ran.quzitech.rnble;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ran on 2016/11/29.
 */
public class MsgCode {
    public static final int BLE_Not_Support = 101;
    public static final int BLE_Not_Enable = 102;
    public static final int Invalid_Peripheral = 103;
    public static final int Peripheral_Not_Found = 104;
    public static final int BluetoothGatt_Is_Null = 105;
    public static final int Connection_Fail = 106;
    public static final int Reading_Fail = 107;
    public static final int Writing_Fail = 108;
    public static final int SetNotify_Fail = 109;
    public static final int BluetoothGattService_Not_Found = 110;
    public static final int Characteristic_Not_Found = 111;
    public static final int Send_SMS_Fail = 112;
    public static final int Parser_Fail = 113;
    public static final int BLE_Is_Busy = 114;

    public static final int Fail_to_Save = 201;
    public static final int Fail_to_read = 202;
    public static final int Unsupport_Request_method = 301;
    public static final int Cancel_By_User = 302;
    public static final int Request_Fail = 303;


    private static final HashMap<Integer, String> map = new HashMap<Integer, String>();
    private static WritableMap errorMap = null;
    private Map successMap = null;
    private static final String MapCode = "code";
    private static final String MapMsg = "message";
    private static StringBuilder sb = null;

    static {
        map.put(BLE_Not_Support, "BLE Not Support");
        map.put(BLE_Not_Enable, "BLE Not Enable");
        map.put(Invalid_Peripheral, "Invalid Peripheral");
        map.put(Peripheral_Not_Found, "Peripheral Not Found");
        map.put(BluetoothGatt_Is_Null, "BluetoothGatt Is Null");
        map.put(Connection_Fail, "Connection Fail");
        map.put(Reading_Fail, "Reading Fail");
        map.put(Writing_Fail, "Writing Fail");
        map.put(SetNotify_Fail, "SetNotify Fail");
        map.put(BluetoothGattService_Not_Found, "BluetoothGattService Not Found");
        map.put(Characteristic_Not_Found, "Characteristic Not Found");
        map.put(Send_SMS_Fail, "Send SMS Fail");
        map.put(Parser_Fail, "Parser Fail");
        map.put(BLE_Is_Busy, "BLE Is Busy");
        map.put(Fail_to_Save, "Fail to Save");
        map.put(Fail_to_read, "Fail to read");
        map.put(Unsupport_Request_method, "Unsupport Request method");
        map.put(Cancel_By_User, "Cancel By User");
        map.put(Request_Fail, "Request Fail");

    }

    private static String getErrorMapMsg(int ErrorCode, String... mCustomInfo) {
        sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < mCustomInfo.length; i++) {
            sb.append(mCustomInfo[i] + ((i == mCustomInfo.length - 1) ? " " : ","));
        }
        sb.append("]");
        return map.get(ErrorCode) + sb.toString();
    }

    public static WritableMap getErrorMap(int ErrorCode, String... customInfo) {
        errorMap = Arguments.createMap();
        errorMap.putInt(MapCode, ErrorCode);
        errorMap.putString(MapMsg, getErrorMapMsg(ErrorCode, customInfo));
        return errorMap;
    }

    public static WritableMap getCustomErrorMap(int ErrorCode, String customInfo) {
        errorMap = Arguments.createMap();
        errorMap.putInt(MapCode, ErrorCode);
        errorMap.putString(MapMsg, customInfo);
        return errorMap;
    }
//    public Map getSuccessMap(String ErrorCode) {
//        successMap = new HashMap<String, Object>();
//        successMap.put("MapCode", 0);
//        successMap.put("MapMsg", ErrorCode);
//        return successMap;
//    }

}